<template>
  <el-dialog
    append-to-body
    :title="info"
    :visible.sync="dialogVisible"
    :close-on-click-modal="false"
    align="left"
    v-loading="currLoading"
    element-loading-text="拼命加载中"
    element-loading-spinner="el-icon-loading"
    element-loading-background="rgba(0, 0, 0, 0.8)"
    width="500px">
    <el-form ref="form" :model="form" label-width="80px" :rules="rules">
      <el-form-item label="物流类型" align="left" prop="expressType">
        <el-select v-model="form.expressType" placeholder="物流类型">
          <el-option
            v-for="(item,index) in tyeArr"
            :key="index"
            :label="item"
            :value="index">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="快递公司" align="left" prop="expressCompany" v-if="form.expressType===0">
        <el-select v-model="form.expressCompany" placeholder="快递公司">
          <el-option
            v-for="(item,index) in expressCompanyArr"
            :key="index"
            :label="item"
            :value="item">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="快递编号" align="left" prop="expressNumber"  v-if="form.expressType===0">
        <el-input v-model="form.expressNumber" style="width: 300px"></el-input>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="dialogVisible = false">取 消</el-button>
      <el-button type="primary" @click="submit">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
export default {
  name: 'SupplierOrderApplySubmitExpressDialog',
  props: {
    successFunction: Function
  },
  data: function () {
    let $this=this
    var validateExpressNumber=function (rule, value, callback) {
      if ($this.checkExpressNumber(value)) {
        callback()
      }else{
        callback(new Error('快递单号 必须是由(A-Za-z0-9)组成，长度在5~50位之间'))
      }
    }
    return {
      info: '供货单-退换货-物流提交',
      callback:null,
      dialogVisible: false,
      currLoading: false,
      tyeArr: dict.OrderSendTypeArr.slice(0, 2),
      expressCompanyArr: dict.ExpressCompanyList,
      form: {
        id: 0,
        expressType: 0,
        expressCompany: '',
        expressNumber: ''
      },
      rules: {
        expressCompany: [
          {required: true, message: '请输入 快递公司', trigger: ['blur', 'change']},
          {min: 1, max: 10, message: '长度在 1 到 10 个字符', trigger: ['blur', 'change']}
        ],
        expressNumber: [
          {required: true, message: '请输入 快递编号', trigger: ['blur', 'change']},
          {validator: validateExpressNumber, trigger: ['blur', 'change']}
        ]
      }
    }
  },
  mounted () {
  },
  methods: {
    open: function (id) {
      this.form.id = id
      this.dialogVisible = true
    },
    submit: function () {
      let $this = this
      if (this.form.type===0){
        if (!$this.form.expressCompany) {
          return $this.$message.error('快递公司 不能为空')
        }
        if (!$this.form.expressNumber) {
          return $this.$message.error('快递单号 不能为空')
        }
      }
      $this.$refs.form.validate((valid) => {
        if (valid) {
          var form = {
            id: $this.form.id,
            expressType: $this.form.expressType,
            expressNumber: $this.form.expressNumber,
            expressCompany: $this.form.expressCompany,
          }
          $this.currLoading = true
          $this.MyRequest(this.ApiDoNameSupplier + 'SupplierOrderApply/submitExpress', form, function () {
            $this.$message.success('操作成功')
            $this.currLoading = false
            $this.dialogVisible = false
            $this.$props.successFunction()
            $this.$forceUpdate()
          }, function () {
            $this.$message.success('操作失败，请重试')
            $this.currLoading = false
            $this.$forceUpdate()
          })
        } else {
          return false
        }
      })
    }
  }
}
</script>

<style scoped>

</style>
